package cn.sdormitory.controller.school;

import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.annotation.IgnoreAuth;
import cn.sdormitory.common.annotation.SysLog;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.enums.BusinessType;
import cn.sdormitory.common.utils.poi.ExcelPoi;
import cn.sdormitory.report.vo.AttenceStatisticsVO;
import cn.sdormitory.school.entity.SchoolDuty;
import cn.sdormitory.school.service.SchoolDutyService;
import cn.sdormitory.smartclass.service.ClassDvService;
import cn.sdormitory.smartclass.vo.ClassDvVo;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 值班操作
 */
@RestController
@Api(tags = "School-duty=> 排班")
@RequestMapping("/school/duty")
public class SchoolDutyController {

    @Autowired
    private SchoolDutyService schoolDutyService;

    @ApiOperation("getClassDvPageList => 分页查询排班")
    @PreAuthorize("@ss.hasPermi('school:duty:query')")
    @PostMapping(value = "/getDutyPageList")
    public CommonResult<CommonPage<SchoolDuty>> getDutyPageList(@RequestBody Map<String, Object> params){
        return CommonResult.success(CommonPage.restPage(schoolDutyService.getPage(params)));
    }

    @ApiOperation("createOrUpdateSchoolDuty => 新增或修改排班信息")
    @PreAuthorize("@ss.hasPermi('school:duty:edit')")
    @PostMapping(value = "/createOrUpdateSchoolDuty")
    public CommonResult<Integer> createOrUpdateSchoolDuty(@RequestBody SchoolDuty schoolDuty){
        int num = schoolDutyService.createOrUpdateSchoolDuty(schoolDuty);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed(500,"排班异常，或排班时间重复");
    }

    @ApiOperation("deleteSchoolDuty => 删除排班信息")
    @PreAuthorize("@ss.hasPermi('school:duty:delete')")
    @PostMapping(value = "/deleteSchoolDutyById")
    public CommonResult<Integer> deleteSchoolDuty(@RequestBody SchoolDuty schoolDuty){
        int num = schoolDutyService.deleteSchoolDuty(schoolDuty.getId());
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("importTemplate => 下载模板")
    @SysLog(title = "学员管理", businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        EasyExcel.write(response.getOutputStream(), SchoolDuty.class).sheet("值班人员").doWrite(null);
    }

    @IgnoreAuth
    @ApiOperation("importData1 => 导入值班信息")
    @SysLog(title = "导入值班信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public CommonResult importDutyData(@RequestParam(value = "upload") MultipartFile upload) throws Exception
    {
        try{
            if(upload == null || upload.getSize() == 0){
                return CommonResult.failed("文件为空!!!");
            }
            List<SchoolDuty> list = new ExcelPoi<SchoolDuty>().importObjectList(upload.getInputStream(), upload.getOriginalFilename(), SchoolDuty.class);
            Iterator<SchoolDuty> iterator = list.iterator();
            int rowNum = 0;
            while(iterator.hasNext()){
                rowNum++;
                SchoolDuty schoolDuty = iterator.next();
                int num = schoolDutyService.importDutyList(schoolDuty);
                if(num==-10){
                    return CommonResult.failed("第"+rowNum+"行,老师姓名有误！");
                }
                if(num==-11){
                    return CommonResult.failed("第"+rowNum+"行,值班时间有重复！");
                }
                if(num==-12){
                    return CommonResult.failed("第"+rowNum+"行,值班部门错误！");
                }
                if(num==-13){
                    return CommonResult.failed("第"+rowNum+"行,值班类型错误！");
                }
            }

        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("导入失败!!!");
        }
        return CommonResult.success("导入成功!!!");
    }

    @ApiOperation("exportDutyData =>导出值班信息")
    @GetMapping(value = "/exportDutyData")
    public CommonResult exportDutyData(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        List<SchoolDuty> list = schoolDutyService.exportDutyList(params);
        try {
            EasyExcel.write(response.getOutputStream(), SchoolDuty.class).sheet("值班记录").doWrite(list);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("updateDutyUser => 删除排班信息")
    @PostMapping(value = "/updateDutyUser")
    public CommonResult<Integer> updateDutyUser(@RequestBody Map<String,Object> map){
        int num = schoolDutyService.updateDutyUser(map);
        if(num>1){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("updateDutyUser => 删除某月排班信息")
    @SysLog(title = "删除日期内排班", businessType = BusinessType.DELETE)
    @PostMapping(value = "/deleteOtherMonthDuty")
    public CommonResult<Integer> deleteOtherMonthDuty(@RequestBody Map<String,Object> map){
        int num = schoolDutyService.deleteOtherMonthDuty(map);
        if(num>=0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
